package top.yihoxu.yapi.manager;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Component;
import top.yihoxu.yapi.config.OssClientConfig;


import javax.annotation.Resource;
import java.io.File;

/**
 * @Author dth
 * @Date 2024/7/9 15:39
 * @PackageName:top.yihoxu.answer.manager
 * @ClassName: OssManager
 * @Description: oss对象存储操作
 * @Version 1.0
 */
@Component
public class OssManager {

    @Resource
    private OssClientConfig ossClientConfig;

    @Resource
    private OSSClient ossClient;

    /**
     * 上传对象
     *
     * @param key 唯一键
     * @param localFilePath 本地文件路径
     * @return
     */
    public PutObjectResult putObject(String key, String localFilePath) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossClientConfig.getBucket(), key,
                new File(localFilePath));
        return ossClient.putObject(putObjectRequest);
    }

    /**
     * 上传对象
     *
     * @param key 唯一键
     * @param file 文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossClientConfig.getBucket(), key,
                file);
        return ossClient.putObject(putObjectRequest);
    }
}
